# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
from ershoufang.items import ErshoufangItem
from bs4 import BeautifulSoup
from ershoufang.public import SpiderPublic as sp

class FangtianxiaSpider(scrapy.Spider):
    name = 'fangtianxia'
    allowed_domains = ['fang.com']

    base_url='http://sz.esf.fang.com'
    bash_url='http://sz.esf.fang.com/house/i3'
    #http://sz.esf.fang.com/house/i32/

    def start_requests(self):
        for i in range(1,101):
            url=self.bash_url+str(i)+'/'
            yield Request(url, self.parse)
            # http://sz.esf.fang.com/house/i32/

    def parse(self, response):
        mainlist=BeautifulSoup(response.text,'html.parser').find_all('dl',class_='clearfix',dataflag='bg')
        for house in mainlist:

            title=house.find('span',class_='tit_shop').text.strip()
            url=house.find('h4',class_='clearfix').find('a')['href']
            rooms=house.find('p',class_='tel_shop').text.strip().split('|')
            huxing=rooms[0].strip()
            #mianji=rooms[1].strip()
            louceng=rooms[2].strip()
            #chaoxiang=rooms[3].strip()
            #year=rooms[4].strip()

            add=house.find('p',class_='add_shop').find('span')
            if add==None:
                address=''
            else:
                address=add.text.strip()

            bash_url=self.base_url+url
            jt=house.find('p',class_='clearfix label').find('span',class_='bg_none icon_dt')

            if jt==None:
                jtms=''
            else:
                jtms=jt.text.strip()

            yield Request(bash_url, callback=self.get_roomDetails, meta={
                'title': title,
                'url': bash_url,
                'huxing': huxing,
                'louceng': louceng,
                'jtms':jtms,
                'address':address
            })

    def get_roomDetails(self,response):
        item=ErshoufangItem()
        item['title']=str(response.meta['title'])
        item['address']=str(response.meta['address'])
        item['jtms'] = str(response.meta['jtms'])
        item['louceng'] = str(response.meta['louceng'])
        detail=BeautifulSoup(response.text,'html.parser')

        room=detail.find_all('div',class_='tr-line clearfix')

        item['zongje']=float(detail.find('div',class_="trl-item_top").find('i').text.strip())

        item['huxing']=room[0].find('div',class_='trl-item1 w146').find('div',class_='tt').text.strip()
        item['mianji']=float(room[0].find('div',class_='trl-item1 w182').find('div',class_='tt').text.strip()[:-2])
        item['danjia']=float(room[0].find('div',class_='trl-item1 w132').find('div',class_='tt').text.strip()[:-4])

        item['chaoxiang'] = room[1].find('div', class_='trl-item1 w146').find('div', class_='tt').text.strip()
        #item['louceng'] = room[1].find('div', class_='trl-item1 w182').find('div', class_='tt').text.strip()
        item['zhuangxiu'] = room[1].find('div', class_='trl-item1 w132').find('div', class_='tt').text.strip()

        xiaoqu_info=detail.find('div',class_='tab-cont-right').find('div',attrs={'class':'tr-line','style':'padding: 23px 0; padding-bottom: 8px;position: relative'})

        qu=xiaoqu_info.find_all('div',class_='trl-item2 clearfix')
        xiaoqu=qu[0].find('div',class_='rcont').find('a',class_='blue')
        if xiaoqu==None:
            item['xiaoqu']=''
        else:
            item['xiaoqu']=qu[0].find('div',class_='rcont').find('a',class_='blue').get_text().strip()

        weizhi=qu[1].find('div',class_='rcont',id='address').find_all('a')

        dist=weizhi[0].get_text().strip()
        item['quyu']=sp.updateDist(dist)
        item['jiedao']=weizhi[1].get_text().strip()

        fangyuan_info=detail.find('div',class_='content-item fydes-item').find('div',class_='cont clearfix').find_all('div',class_='text-item clearfix')

        item['zhuzhaileixing']=''
        item['year']=''
        item['cqxz']=''

        for fangyuan in fangyuan_info:
            #print(fangyuan)
            fang=fangyuan.find_all('span')
            if fang[0].text.strip()=='建筑年代':
                item['year']=fang[1].text.strip()[:-1]
                #print(year)

            if fang[0].text.strip()=='住宅类别':
                item['zhuzhaileixing']=fang[1].text.strip()
                #print(zhuzhaileixing)

            if fang[0].text.strip()=='产权性质':
                item['cqxz']=fang[1].text.strip()
                #print(cqxz)

        item['laiyuan']='房天下'

        return item
